import file_list as fl 
import time
import numpy as np
import matplotlib.pyplot as plt
import cv2
import re
#------------------------------------------------
Name_list=fl.GetFileList('./img/1105','PNG')
#------------------------------------------------
def get_gray_value(img,pos,harf_d):
    sum = 0
    count = 0
    start_point=[0,0]
    end_point = [0,0]
    start_point[0]=pos[0]-harf_d
    start_point[1]=pos[1]-harf_d

    end_point[0]=pos[0]+harf_d
    end_point[1]=pos[1]+harf_d
    #print(start_point[0],end_point[0])
    for i in range(start_point[0],end_point[0]+1):#行遍历
        #print(i)
        for j in range(start_point[1],end_point[1]+1):#行遍历
            #print(i,j)
            sum += img[i,j]
            count += 1
    avg=sum/count
    return img[pos[0]][pos[1]],round(avg,2)


#鼠标回调函数 
#事件 坐标 标志 返回

def show_num(event,y,x,flags,param):
    d=80
    if event==cv2.EVENT_LBUTTONUP:
        #print('POS:(',x,y,') R:',r,'G:',g,'B:',b)
        #print('POS:(',x,y,')' ,img.item(x,y,0),img.item(x,y,1),img.item(x,y,2))
        print('POS:(',x,y,')' ,r[x][y])
        ave=get_gray_value(r,[x,y],d)
        print('average :(',x,y,')' ,ave)
        #cv2.circle(img,(y,x),d*2,(0,255,0),1)
        cv2.rectangle(img,(y-d,x-d),(y+d,x+d),(255,255,0))
        cv2.putText(img,str(ave[1]),(y,x), cv2.FONT_HERSHEY_SIMPLEX, 1,(255,255,0),1)
    elif event==cv2.EVENT_MBUTTONDOWN:
        print(num)


    
#----------------------------------------------------------------------


print(Name_list)
#pos=[221,1419]#G1 700 
#pos=[255,1540]#G1 740 
#pos=[221,1248]#G2 700 
#pos=[255,1540]#G2 740 

#
pos=[449,1024]
d=5

result=[]  #brightness , DN

for namelist in Name_list:
    single_name=namelist.split('\\')[1]
    #read_zone(namelist,[100,100],5)
    cv2.namedWindow('image')
    img = cv2.imread(namelist,1)
    #print('img info:',src_path)
    #print('shape:',img.shape,' pixal num:',img.size,' dtype:',img.dtype)
    zeros = np.zeros(img.shape[:2], dtype = "uint8")
    #RGB拆分
    b,g,r=cv2.split(img)
    #plt.hist(img.ravel(),256,[0,256]);
    #plt.show()
    #cv2.imshow('image',img)
    #cv2.imshow('image',r)
    #cv2.imshow('image3',(r+g)/(r-g))
    #cv2.imshow('image_B',cv2.merge([b, zeros, zeros]))#带颜色的单通道灰度图
    #cv2.imshow('image_G',cv2.merge([zeros, g, zeros]))#带颜色的单通道灰度图
    #cv2.imshow('image_R',cv2.merge([zeros, zeros, r]))#带颜色的单通道灰度图
    cv2.setMouseCallback('image',show_num)
    
    x=pos[0]
    y=pos[1]
    ave=get_gray_value(r,[x,y],d)
    #print('average :(',x,y,')' ,ave)
    result.append(single_name)
    ele=re.split('[. ]',single_name)
    bri_str=ele[0]+'.'+ele[1]
    result.append(bri_str)
    result.append(ave[0])
    result.append(ave[1])
    d=80
    cv2.rectangle(img,(y-d,x-d),(y+d,x+d),(0,255,0))
    cv2.putText(img,str(ave[1]),(y,x), cv2.FONT_HERSHEY_SIMPLEX, 1,(255,255,0),1)
    cv2.putText(img,namelist,(20,30), cv2.FONT_HERSHEY_SIMPLEX, 1,(174,255,0),1)
    while(1):
        cv2.imshow('image',img)

        key=cv2.waitKey(1)&0xFF
        if key==27:
            break
        #time.sleep(0.02)
        #break
cv2.destroyAllWindows()       
print('='*20)
#print(result)
res_mat=np.array(result).reshape(len(Name_list),4)  # reshape(列的长度，行的长度)
print(res_mat)
